<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="唯一标识：">
            <el-input
              style="width: 203px"
              v-model="listQuery.unid"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="发布者ID：">
            <el-input
              style="width: 203px"
              v-model="listQuery.uuid"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="发布者昵称：">
            <el-input
              style="width: 203px"
              v-model="listQuery.userNickname"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="动态内容：">
            <el-input
              style="width: 203px"
              v-model="listQuery.content"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-tabs v-model="statusTab" type="card" @tab-click="handleClickTabs">
        <el-tab-pane
          :label="'全部(' + countMap.allCount + ')'"
          name="-1"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先发后审)(' + countMap.issueReviewCount + ')'"
          name="10"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先审后发)(' + countMap.reviewIssueCount + ')'"
          name="20"
        >
        </el-tab-pane>
        <el-tab-pane
          :label="'已下架(' + countMap.offTheShelfCount + ')'"
          name="40"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核通过(' + countMap.approvedCount + ')'"
          name="30"
        ></el-tab-pane>

        <el-tab-pane
          :label="'审核未通过(' + countMap.notPassedCount + ')'"
          name="0"
        ></el-tab-pane>
      </el-tabs>
      <el-table
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="唯一标识" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.unid }}</template>
        </el-table-column>
        <el-table-column label="动态内容" align="center">
          <template slot-scope="scope" >
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column label="点赞量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.likedCount }}</template>
        </el-table-column>
        <el-table-column label="评论量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.commentCount }}</template>
        </el-table-column>
        <el-table-column label="分享量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.shareCount }}</template>
        </el-table-column>
        <el-table-column label="评论时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="发布者" width="100" align="center">
          <template slot-scope="scope"
            ><a>{{ scope.row.userNickname }}</a>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="130" align="center">
          <template slot-scope="scope">
            <p
              :class="
                scope.row.status === 30 ? 'success-status' : 'error-status'
              "
            >
              {{ scope.row.status | statusFilter }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                @click="handleVerify(scope.$index, scope.row)"
                >审核
              </el-button>
              <!-- <el-button
                size="mini"
                type="primary"
                @click="handleViewDetail(scope.$index, scope.row)"
                >查看详情
              </el-button> -->
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
      <div style="height: 20px"></div>
    </div>
    <el-dialog title="审核" :visible.sync="verifyDialogVisible" width="30%">
      <el-form :model="verifyForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="verifyForm.verifyStatus" size="medium">
            <el-radio :label="40">下架</el-radio>
            <el-radio :label="30">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="verifyForm.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVerifySubmit" size="small"
          >确 定</el-button
        >
        <el-button @click="verifyDialogVisible = false" size="small"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, getCounts, updateStatus } from "@/api/news";
import { getMediaTypes } from "@/api/enum";

const defaultListQuery = {
  uuid: null,
  userNickname: "",
  unid: null,
  status: null,
  content: null,
  pageNum: 1,
  pageSize: 10,
};
export default {
  name: "productList",
  data() {
    return {
      mediaTypeOptions: [],
      operates: [
        {
          label: "批量审核",
          value: "verify",
        },
      ],
      operateType: "verify",
      multipleSelection: [],
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,

      statusTab: "-1",
      countMap: {
        allCount: 0,
        offTheShelfCount: 0,
        approvedCount: 0,
        reviewIssueCount: 0,
        issueReviewCount: 0,
        notPassedCount: 0,
      },
      verifyDialogVisible: false,
      verifyForm: {
        ids: [],
        verifyStatus: 30,
        detail: "",
      },
    };
  },
  created() {
    this.getMediaTypeOptions();
    this.getCountMap();
    this.getList();
  },
  filters: {
    statusFilter(value) {
      if (value === 40) {
        return "已下架";
      } else if (value === 30) {
        return "审核通过";
      } else if (value === 20) {
        return "待审核";
      } else if (value === 10) {
        return "待审核";
      } else {
        return "审核未通过";
      }
    },
    mediaTypeFilter(value) {
      if (value === 0) {
        return "视频";
      } else if (value === 1) {
        return "动态";
      } else if (value === 3) {
        return "文章";
      }
    },
  },
  methods: {
    getMediaTypeOptions() {
      getMediaTypes().then((res) => {
        this.mediaTypeOptions = res.data;
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getCountMap() {
      getCounts(this.listQuery).then((res) => {
        this.countMap = res.data;
      });
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.statusTab = "-1";
      this.getList();
      this.getCountMap();
    },
    handleClickTabs(tab, event) {
      var status = Number(this.statusTab);
      if (status !== -1) {
        this.listQuery.status = status;
      } else {
        this.listQuery.status = null;
      }
      this.getList();
      this.getCountMap();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
      this.getCountMap();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
      this.getCountMap();
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleViewDetail(index, row) {
      this.$router.push({
        path: "/cms/commentDetail",
        query: { id: row.id },
      });
    },
    handleVerify(index, row) {
      this.verifyDialogVisible = true;
      this.verifyForm.ids = [row.id];
    },
    handleVerifySubmit() {
      let params = new URLSearchParams();
      params.append("ids", this.verifyForm.ids);
      params.append("verifyStatus", this.verifyForm.verifyStatus);
      params.append("detail", this.verifyForm.detail);
      updateStatus(params).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
        this.verifyDialogVisible = false;
        this.getList();
        this.getCountMap();
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的评论",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let ids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].id);
      }
      this.verifyDialogVisible = true;
      this.verifyForm.ids = ids;
    },
  },
};
</script>
<style>
.success-status {
  background-color: #67c23a;
  color: white;
}
.error-status {
  background-color: #f56c6c;
  color: white;
}
</style>


